<?php
/*
模板生成类
@author jackbrown;
@time 2012-11-05
模板生成主要包括，对模板中模板标记的解板
流程控制解板主要有:if foreach 和 dbfor等流程块的解析
环境变量的解析:session,cookie和其它变量
系统插件的解析如textbox,msgbox等
支持cache缓存,插件和即时输出和gzip输出
@version 1.0.16
require('global.php');
$t = new template();
$t->cache = true;
$t->assign('title','测试标题');
$t->assign('header','标题sss一');
$t->assign('job','vip');

$cacheCom->cacheType = 'array_file';
$cacheCom->cacheKey = 'user';
$cacheCom->expires = 10;

$stime = run_time();

if(!$t->isCached('test.html')){
	if(!$cacheCom->isCached()){
		
		$res = $con->query('select*from happy_product_infor limit 10');
		$etime = run_time();
	}else{
		echo 'cached';
		$res = $cacheCom->readCache();	
		$etime = run_time();
	}

	$t->assign('res',$res);
}
$etime = run_time();
echo $etime - $stime;

$list = array( array('name'=>'yhbhiron'),array('name'=>'john'),array('name'=>'jack'));
$t->assign('List',$list);
$t->assign('body','<h1> This is my body</h1>');
$t->assign('my',array('name'=>'yhb'));
$t->display('test.html');
*/
if(!defined('IN_WEB')){
	exit('Access Deny!');
}

class template{
	
	/*左分隔符*/
	public $leftSpe	= '<{';
	
	/*右分隔符*/
	public $rightSpe = '}>';
	
	/*是否可缓存,将编译后文件直接生成html*/
	public $cache = false;
	
	/*可缓存时间,单位s*/
	public $cacheTime = 10;
	
	/*缓存路径*/
	public $cacheDir = CACHE_DIR;
	
	/*模板路径*/
	public $tplDir = TEMPLE_DIR;
	
	/*编译文件路径*/
	public $compDir = COMP_DIR;
	
	public $htmlCode = '';
	
	/*插件目录*/
	public $pluginDir = PLUG_DIR;
	
	/*变量数组*/
	private $vars = array();
	private $_foreach = array();
	private $cacheObj;
	private $_includes = array();
	private static $isLoad = false;
	private $_plugins = array();
	
	public function __construct(){
		
		if(self::$isLoad){
			return false;
		}
		
		if(!file_exists($this->cacheDir)){
			
			$this->error('缓存目录不存在!');
		}
		
		if(!file_exists($this->tplDir)){
			
			$this->error('模板目录不存在!');
		}
		
		if(!file_exists($this->compDir)){
			
			$this->error('编译路径目录不存在!');
		}		
		

		
		if(!class_exists('cache')){
			$this->cache = false;
		}else{
		
			$this->cacheObj = new cache();
		}
		
	}
	
	
	/**
	*显示模板
	*@param string $tpl 模板文件
	*@param cache 是否需要缓存
	*/
	public function display($tpl,$cache=false,$time=10,$inc=false){
		
		$tplPath = $this->tplDir.'/'.$tpl;
		
		if(!file_exists($tplPath)){
						
			$this->error('找不到模板文件'.$tpl);
			return false;
		}

		$needCache = $this->cache == true && $cache == true;
		$this->nedCache = $needCache;
				
		/*调试信息*/
		if($inc){
			$this->_includes[] = $tpl;
			if(DEBUG){
				$stime = run_time();
				$GLOBALS['debugStr'][] = '引用模板：'.$tpl.'; 缓存:'.($needCache ? '有' : '无');
			}

		}else{
			if(DEBUG){
				$stime = run_time();
				$GLOBALS['debugStr'][] = '当前模板：'.$tpl.'; 缓存:'.($needCache ? '有' : '无');
			}
		}
		
		
		$compPath = $this->compDir.'/'.urlencode($tpl).'.php';	
		if(USE_GZIP == false){
			flush();
		}
		
		
		if($needCache && $this->isCached($tpl)){
			
			if(USE_GZIP == false){
				ob_implicit_flush();
			}
			
			$this->cacheObj->cacheType = 'html_file';
			$this->cacheObj->cacheKey  = urlencode($tpl);
			echo($this->cacheObj->readCache());
			$content = ob_get_contents();
			
			if(!$inc){
				debugWrite();
			}
			
			return $content;
		}
		
		if($needCache){
			$this->cacheObj->expires = $time;
		}
		
		if(!$this->isModified($tpl)){
			if($needCache){
				
				if(!$inc){
					require($compPath);	
				}else{
					include($compPath);	
				}
				
				$content = ob_get_contents();				
				$this->cacheObj->cacheType = 'html_file';
				$this->cacheObj->cacheKey  = urlencode($tpl);
				$this->cacheObj->writeCache();
				
				if(!$inc){
					if(DEBUG){
						$GLOBALS['debugStr'][] = '模板未改变，不需要编译，且使用了缓存';	
					}
				    debugWrite();
					ob_end_flush();
				}
				
			}else{
				
				if(!$inc){
					require($compPath);	
				}else{
					include($compPath);	
					if(USE_GZIP == false){
						
						flush();
					}
				}
				
				$content = ob_get_contents();
				if(!$inc){
					
					if(DBUG){
						$GLOBALS['debugStr'][] = '模板未改变，不需要编译,直接引用编译后文件';
					}
					
				    debugWrite();
					ob_end_flush();
				}
			}
			
		}else{
			
			if(DBUG){
				$GLOBALS['debugStr'][] = '模板已改变，需要得新编译';
			}
			
			if($needCache){

				$this->compile($tpl,$needCache,$time);
				$this->cacheObj->cacheType = 'html_file';
				$this->cacheObj->cacheKey  = urlencode($tpl);
				$this->cacheObj->writeCache();
				
				$content = ob_get_contents();
				if(!$inc){
				    debugWrite();
					ob_end_flush();
				}
				
			}else{
				$this->compile($tpl,$needCache,$time);
				$content = ob_get_contents();
				if(!$inc){
				    debugWrite();
					ob_end_flush();
				}
			}
		
		}
		
		if(DEBUG){
			$etime = run_time();
			$GLOBALS['debugStr'][] = '模板'.$tpl.'执行时间:<font color="green">
			(时间:'.(($etime - $stime)*1000).' ms)</font>';
		}
		
		return $content;
	}
	
	/**
	*是否存在缓存
	*/
	public function isCached($tpl){
		
		if($this->cache == false || $this->isModified($tpl)){
			return false;
		}
		
		$tplPath  = $compPath = $this->compDir.'/'.urlencode($tpl).'.php';
		if(!file_exists($tplPath)){
			return false;
		}
		
		$headers = $this->readHeader($tplPath);


		$this->cacheObj->expires = $headers['cache-time'];
		$this->cacheObj->cacheType = 'html_file';
		$this->cacheObj->cacheKey  = urlencode($tpl);		
		return $this->cacheObj->isCached();
		
	}
	
	
	/**
	 * 添加模板变量
	*@param string $varName 变量名称
	*@param mixed $val 变量值
	*/
	public function assign($varName,$val){
		
		$this->vars[$varName] = $val;
	}
	
	/**
	*清除所有变量
	**/
	public function clearAllVars(){
	
		unset($this->vars);
	}
	
	
	public function __destruct(){
		
		$this->clearAllVars();	
	}
	
	/**
	*编译文件
	**/
	protected function compile($tpl,$cache=false,$cacheTime=0){
		
		$code = file_get_contents($this->tplDir.'/'.$tpl);
		if($code == ''){
			return false;	
		}
        		
		/*加载插件*/
		if(file_exists($this->pluginDir)){
			$this->loadPlugins();
		}
		
		$code = $this->toTargetCode($code);	
		$header = $this->addHeader($this->tplDir.'/'.$tpl,$cache,$cacheTime);		
		file_put_contents($this->compDir.'/'.urlencode($tpl).'.php',$header.$code,LOCK_EX);
		
		try{
			require($this->compDir.'/'.urlencode($tpl).'.php');
		}catch(Exception $e){
			$err  = $e->getTrace();
			foreach($err as $k=>$er){
			 echo '文件:',$er['file'],';行:',$er['line'],";错误:",$e->getMessage(),"<br />";
			
			}
		}
	}
	
	
	/**
	 * 获取编译后的php代码
	 * @param string $code 模板代码内容
	 * @return string 
	 */
	protected function toTargetCode($code){
		
		if($code==null || $code==''){
			return '';	
		}
		
		$code = $this->compilePlugins($code);
		preg_match_all('/'.$this->leftSpe.'\s*(.+?)\s*'.$this->rightSpe.'/is',$code,$tagBlock);

		if($tagBlock[1]!= null){
		  
			  foreach($tagBlock[0] as $key=>$c){
					
					$temp = $this->compileVars($c);
					
					$temp = $this->compileIfTag($temp);
					$temp = $this->compileforeachTag($temp);
					$temp = $this->compileInclude($temp);
					$temp = $this->compiledbforTag($temp);
					$temp = $this->compileFunc($temp);
					$temp = $this->compileScript($temp);
					$temp = $this->compileCss($temp);
					
					
					$temp = $this->compileTag($temp);
					$code = str_replace($c,$temp,$code);
					$code = $this->language($code);
			 }
						
		}
		
		return $code;
		
	}
	
	/**
	*给编译文件添加头部信息
	*@param string $tpl模板文件
	*@param boolean $cache 是否可缓存此文件true/false;
	*@return string 设置的头部信息字符
	*/
	protected function addHeader($tpl,$cache=false,$cacheTime=0){
	
		$time = filemtime($tpl);
		
		$header = "<?php\n/*
		@create-time:$time
		@cache:".intval($cache)."
		@cache-time:$cacheTime
		*/\n
		if(!defined('IN_WEB')){
			exit('Access Deny!');
		}
		
		if(\$cache && \$this->needCache){
			ob_flush();
		}
		?>\n";
				  
		return $header;
	}
	
	/**
	*读取编译文件的头部信息
	*@param string $tpl编译文件
	*@return array头部信息数组
	*/
	protected function readHeader($tpl){
		
		$fh = fopen($tpl,'r');
		$headers = array();
		
		while(!feof($fh)){
			
			$lineCode = trim(fgets($fh));
			if(substr($lineCode,0,1) == '@'){
				
				$hItem = explode(':',$lineCode);
				$headers[substr($hItem[0],1)] = $hItem[1];
			}
			
			if($lineCode == '?>'){
				
				break;
			}
		}
		
		fclose($fh);
		return $headers;
	}

	
	/**
	*加载插件函数
	*/
	protected function loadPlugins(){

		if(is_dir($this->pluginDir)){
			$sub = scandir($this->pluginDir);
			$temp = array();
			foreach($sub as $k=>$fname){
				
				if($fname!='.' && $fname!='..'){	
				
					$fullName = $this->pluginDir.'./'.$fname;
					$p = $fullName;
					
					if(is_file($p)){
						$f = basename($p);
						
						if(preg_match('/^hiron\.([a-z_]+)\.php$/i',$f,$m)){
							
							$funcName = "plugin_".$m[1];
							if(function_exists($funcName)){
								
								$this->plugins[$funcName] = $funcName;
								
							}else{
								
								@include($p);
								if(function_exists($funcName)){
									$this->plugins[$funcName] = $funcName;
									
									if(DEBUG){
										$GLOBALS['debugStr'][] = '加载插件:'.$m[1];
								 	}
								}
							}
							
						}//eof is preg_match
			
					}//eof is file
				}//eof is not .
			
			}//eof foreach
		}//eof is_dir
	
	}
	
	/**
	*替换左右分隔符
	**/
	protected function compileTag($code){
		
		$code = str_replace($this->leftSpe,'<?php ',$code);
		$code = str_replace($this->rightSpe,' ?>',$code);		
		return $code;
		
	}
	
	
	
	/**
	*编辑变量
	**/
	protected function compileVars($code){
		
		/*给变量加echo*/
		$code = preg_replace('/'.$this->leftSpe.'\s*(\$[^=]+)\s*'.$this->rightSpe.'/is',
														$this->leftSpe.'echo $1'.$this->rightSpe,
														$code
				);
		/*foreach*/
		$code = preg_replace('/\$hiron\.foreach\.([a-zA-Z0-9_\-]+)\.iteration/i','$this->_foreach[\'$1\'][\'iteration\']',$code);
		/*session*/
		$code = preg_replace('/\$hiron\.session\.([a-zA-Z0-9_\-:%\$]+)/i','$_SESSION[\'$1\']',$code);
		
		$code = preg_replace('/\$hiron\.get\.([a-zA-Z0-9_\-:%\$]+)/i','$_GET[\'$1\']',$code);
		
		/**超级变量**/
		$code = preg_replace('/\$hiron\.post\.([a-zA-Z0-9_\-:%\$]+)/i','$_POST[\'$1\']',$code);
		$code = preg_replace('/\$hiron\.cookie\.([a-zA-Z0-9_\-:%\$]+)/i','base64_decode($_COOKIE[\''.PRE.'$1\'])',$code);
		$code = preg_replace('/\$hiron\.server\.([a-zA-Z0-9_\-]+)/i','$_SERVER[\'$1\']',$code);
		$code = preg_replace('/\$hiron\.global\.([a-zA-Z0-9_\-]+)/i','$GLOBALS[\'$1\']',$code);
		$code = preg_replace('/\$hiron\.class\.([a-zA-Z0-9_\-]+)::/i','$this->vars[\'$1\']->',$code);
		$code = preg_replace('/\$hiron\.request\.([a-zA-Z0-9_\-]+)/i','$_REQUEST[\'$1\']',$code);
		
		/*数组变量*/
		$code = preg_replace('/\$([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/i','$this->vars[\'$1\'][\'$2\'][\'$3\'][\'$4\']',$code);
		$code = preg_replace('/\$([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/i','$this->vars[\'$1\'][\'$2\'][\'$3\']',$code);
		$code = preg_replace('/\$([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/i','$this->vars[\'$1\'][\'$2\']',$code);
		
		/*普通变量*/
		$code = preg_replace('/(?<!\'|\")\$(?!this|_SESSION|_REQUEST|_COOKIE|_GET|_POST|_SERVER)([a-zA-Z0-9_\-]+)/i','$this->vars[\'$1\']',$code);
		
		return $code;
	}
	
	
	/**
	*编译插件
	**/
	protected function compilePlugins($code){
		
		if($this->plugins == null){
			return $code;
		}
		
		$temp = '';
		foreach($this->plugins as $func){
			
			$temp = $func($code,$this);	
			if($temp!=''){
				$code = $temp;	
			}
		}
		
		if($temp == ''){
			return $code;
		}
		return $temp;
	}
	
	/**
	*编译if标签
	**/
	protected function compileIfTag($code){
		
		$code = preg_replace('/(?<!\/|else)\s*if(.+)\s*'.$this->rightSpe.'/i','if($1){'.$this->rightSpe,$code);
		$code = preg_replace('/'.$this->leftSpe.'\s*(else)\s*'.$this->rightSpe.'/',$this->leftSpe.'}$1{'.$this->rightSpe,$code);
		$code = preg_replace('/'.$this->leftSpe.'\s*elseif\s*(.+)\s*'.$this->rightSpe.'/',$this->leftSpe.'}else if($1){'.$this->rightSpe,$code);
		$code = preg_replace('/\s*\/if\s*/i','}',$code);
		
		return $code;
		
	}
	
	
	/**
	*编译function
	*/
	protected function compileFunc($code){
		
		$code = preg_replace('/'.$this->leftSpe.'\s*([a-z0-9A-Z_]+\(.*\))\s*'.$this->rightSpe.'/is',
				$this->leftSpe.'echo $1'.$this->rightSpe,$code);
		return $code;
	}
	
	
	/**
	*编译foreach标签
	*针对标签 <{foreach item=xx form=$xx }>
	*/
	protected function compileforeachTag($code){

		if(preg_match('/'.$this->leftSpe.'\s*foreach (.+)\s*'.$this->rightSpe.'/i',$code,$forCode)){
			
			$forItem = preg_split('/\s+/',$forCode[1]);
			$forArray = array();
			
			foreach($forItem as $k=>$v){
				
				if(trim($v) != ''){
					$each = preg_split('/\s*=\s*/',trim($v));
					$forArray[trim($each[0])]  = trim($each[1]);
				}
				
			}
			
			if($forArray['from'] == null){
				$this->error('foreach的变量需为一数组!');	
			}		
			
			$vName = $forArray['item'] =='' ? 'v' : $forArray['item'];
			$kName = $forArray['key'] == '' ? 'k' : $forArray['key'];
			$forName = $forArray['name'] == '' ? $vName : $forArray['name'];

			$forStr = $this->leftSpe." \$array = ".$forArray['from']. ";
			\$this->_foreach['$forName']['iteration'] = 0;
			if(\$array != null){
				if(USE_GZIP == false){
					flush();
				}
				foreach(\$array as \$this->vars['$kName']=>\$this->vars['$vName']){ 
					\$this->_foreach['$forName']['iteration']++;
					if(USE_GZIP == false){
						flush();
					}
				".$this->rightSpe;
				
			$code = str_replace($forCode[0],$forStr,$code);
			
		}
		$code = preg_replace('/\s*\/foreach\s*/i'," }}\n unset(\$array);",$code);
		return $code;
	}
	
	
	function readTagParams($paramCode){
		$items = preg_split('/\s+/',$paramCode);
		$params = array();
		foreach($items as $k=>$v){
			if(trim($v)!=''){
				preg_match('/^([a-z0-9_]+)=([^\s]+)/i',trim($v),$match);
				$params[trim($match[1])] = $this->readTagValue(trim($match[2]));
			}
		}
		
		return $params;
	}
	
	/**
	 * 未定
	 */
	function readTagValue($value){
		if(preg_match('/[\'\"]([\s\S]+)[\'\"]/',$value,$match)){
			if(strstr($match[1],'$')){
				return preg_replace('/^[\'\"]|[\'\"]$/','',$value);
			}
		}
		
		return $value;
	}
	
	/**
	*编译数据库读取
	*兼容foreach模式
	*针对标签 <{dbfor item=xx form=$xx cache=true/false cache_key=xxx }>
	*/
	protected function compiledbforTag($code){
		if(preg_match('/'.$this->leftSpe.'\s*dbfor (.+)\s*'.$this->rightSpe.'/i',$code,$forCode)){
			
			$forItem = preg_split('/\s+/',$forCode[1]);
			$forArray = array();
			
			foreach($forItem as $k=>$v){
				
				if(trim($v) != ''){
					$each = preg_split('/\s*=\s*/',trim($v));
					$forArray[trim($each[0])]  = trim($each[1]);
				}
				

			}
			
			if($forArray['from'] == null){
				$this->error('dbfor的变量不能为空!');	
			}		
			
			$vName = $forArray['item'] =='' ? 'dbv' : $forArray['item'];
			$kName = $forArray['key'] == '' ? 'dbk' : $forArray['key'];
			$forName = $forArray['name'] == '' ? $vName : $forArray['name'];
			$cache = $forArray['cache'] == 'true' ? 'true' : 'false';
			$cacheKey = $forArray['cache_key'];

			$forStr = $this->leftSpe." \$res = ".$forArray['from']. ";
			\$needCache = $cache;
			\$cacheKey = '$cacheKey';
			
			if(\$needCache == true && \$cacheKey!='' ){
				 \$this->cacheObj->cacheType = 'array_file';
				 \$this->cacheObj->cacheKey = \$cacheKey;
				 
			}
			if(\$res){
				\$this->_foreach['$forName']['iteration'] = 0;
				\$array = array();
				if(USE_GZIP == false){
					flush();
				}
				while(\$this->vars['$vName'] = (is_array(\$res) ? current(\$res) : mysql_fetch_assoc(\$res))){
					\$this->_foreach['$forName']['iteration']++;
					array_push(\$array,\$this->vars['$vName']);
					is_array(\$res) && next(\$res);
					if(USE_GZIP == false){
						flush();
					}
				".$this->rightSpe;
				
			$code = str_replace($forCode[0],$forStr,$code);
			
		}
		
		$code = preg_replace('/\s*\/dbfor\s*/i',
				" }
					if(\$needCache == true && \$cacheKey!='' && !\$this->cacheObj->isCached() ){
						
						\$this->cacheObj->writeCache(\$array);
						
					}
					unset(\$res);
					unset(\$array);
				}else{ 
					\$this->error('dbfor数据源不是数组或查询结果');
				} \n",
				$code);
		return $code;
	}
	
	
	
	
	/**
	*include file
	*<{include tpl='xx' cache=true/false time=number }>
	*/
	protected function compileInclude($code){
		
		if(preg_match('/'.$this->leftSpe.'\s*include (.+)\s*'.$this->rightSpe.'/',$code,$inCode)){
													  
			$incItem = preg_split('/\s+/',$inCode[1]);
			$incArray = array();
			
			foreach($incItem as $k=>$v){
				
				if(trim($v) != ''){
					$each = preg_split('/\s*=\s*/',trim($v));
					$incArray[trim($each[0])]  = trim($each[1]);
				}
				

			}
			
			if($incArray['tpl'] == null){
				$this->error('include:tpl 不能为空!');	
			}
			
			$cache = $incArray['cache']!='true' ? 'false' : 'true';	
			$time  = !is_numeric($incArray['time']) ? $this->cacheTime : intval($incArray['time']);	
			$incArray['tpl'] = preg_replace('/\'|\"/','',$incArray['tpl']);
			if(substr($incArray['tpl'],0,1) == '$' ){
				$incArray['tpl'] = '$this->vars[\''.substr($incArray['tpl'],1).'\']';	
			}else{
				$incArray['tpl'] = "'".$incArray['tpl']."'";
			}
			
			$incStr = $this->leftSpe." \$this->display(".$incArray['tpl'].",$cache,$time,true);".$this->rightSpe;
			$code = str_replace($inCode[0],$incStr,$code);
		}
		
		return $code;
	}
	
	
	
	/**
	*编译script
	*<{script src='xx,'xx' }>
	*/
	protected function compileScript($code){
		
		if(preg_match('/'.$this->leftSpe.'\s*script (.+)\s*'.$this->rightSpe.'/',$code,$scriptCode)){
													  
			$scriptItem = preg_split('/\s+/',$scriptCode[1]);
			$scriptArray = array();
			
			foreach($scriptItem as $k=>$v){
				
				if(trim($v) != ''){
					$each = preg_split('/\s*=\s*/',trim($v));
					$scriptArray[trim($each[0])]  = trim($each[1]);
				}
				
			}
						
			if($scriptArray['src'] == null){
				
				$this->error('引入的脚本文件不能为空!');
				return false;
			}
			
			$src      = preg_replace('/\'|\"/','',$scriptArray['src']);	
			$compress = $scriptArray['compress'] == 'true' ? 'true' : 'false';
			$merge    = preg_replace('/\'|\"/','',$scriptArray['merge_file']);
			
			if(strstr($src,',')){
				
				$temp = explode(',',$src);
				$files = '';
				
				if($merge != ''){
				
					$files = mergeFiles($temp,$merge);
					
				}else{
					
					foreach($temp as $k=>$file){
						
						if(is_file($file) && file_exists($file)){
							$mtime = filemtime(ROOT.$file);
							$files.="<script type=\"text/javascript\" src=\"$file?v=$mtime\" ></script>";
						}
					}
					
				}
				
				if($files == ''){
					
					$this->error('script:引入的脚本文件不存在!');
					return false;
				}
			}else{
				$mtime = filemtime(ROOT.$src);
				$files = "<script type=\"text/javascript\" src=\"$src?v=$mtime\" ></script>";
			}
			
			$code = str_replace($scriptCode[0],$files,$code);
		}
		
		return $code;
	}
	
	/**
	 * 标签<{css href='xx,xx...' }>
	 */
	protected function compileCss($code){
		
		if(preg_match('/'.$this->leftSpe.'\s*css (.+)\s*'.$this->rightSpe.'/',$code,$cssCode)){
													  
			$cssItem = preg_split('/\s+/',$cssCode[1]);
			$cssArray = array();
			
			foreach($cssItem as $k=>$v){
				
				if(trim($v) != ''){
					$each = preg_split('/\s*=\s*/',trim($v));
					$cssArray[trim($each[0])]  = trim($each[1]);
				}
				
			}
						
			if($cssArray['href'] == null){
				
				$this->error('引入的css文件不能为空!');
				return false;
			}
			
			
			$src      = preg_replace('/\'|\"/','',$cssArray['href']);	
			$compress = $csstArray['compress'] == 'true' ? 'true' : 'false';
			$merge    = preg_replace('/\'|\"/','',$cssArray['merge_file']);
			
			if(strstr($src,',')){
				
				$temp = explode(',',$src);
				$files = '';
				
				if($merge != ''){
				
					$files = mergeFiles($temp,$merge);
					
				}else{
					
					foreach($temp as $k=>$file){
						
						if(is_file($file) && file_exists($file)){
							
							$files.="<link rel=\"stylesheet\" type=\"text/css\" href=\"$file\" />";
						}else{
						
							echo "Warning:$file不存在!";	
						}
					}
					
				}
				
				if($files == ''){
					
					$this->error('script:引入的脚本文件不存在!');
					return false;
				}
			}else{
				
				$files = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$src\" />";
			}
			
			$code = str_replace($cssCode[0],$files,$code);
		}
		
		return $code;
	}
	
	
	/**
	*语言包
	**/
	protected function language($code){
		
		if(!file_exists(ROOT.'./language/'.BASE_LAN.'.php')){
			return $code;
		}
		
		@include(ROOT.'./language/'.BASE_LAN.'.php');
		if($langs!=null && is_array($langs)){
			
			foreach($langs as $s=>$d){
				
				$code = str_replace($s,$d,$code);
			}
		}
		
		return $code;
	}
	
	/**
	*模板文件是否已修改
	*@param $tpl 模板名称
	*@return true/false
	*/
	public function isModified($tpl){
		
		$compPath = $this->compDir.'/'.urlencode($tpl).'.php';
		$tplPath  = $this->tplDir.'/'.$tpl;

		if(!file_exists($compPath)){
			
			return true;
		}
		
		$header = $this->readHeader($compPath);
		if($header['create-time']<filemtime($tplPath)){
		
			return true;
		}
		
		return false;
	}
	
	/**
	异常报告
	**/
	private function error($exception){	
	
		if(class_exists('error') && is_object($GLOBALS['error'])){			
			$GLOBALS['error']->showErrorStr('class templates:'.$exception,'javascript:',false);
		}else{
			throw new Exception($exception);
		}
	}
	
}



?>